﻿namespace TVersityDotNet.MediaLib
{
    using System;
    using System.Collections.Generic;
    using System.Globalization;
    using System.Runtime.Serialization;
    
    /// <summary>
    ///  Samll enumeration representing the different flag options when browsing
    /// </summary>
    public enum BrowseFlagOptions
    {
        BrowseMetaData, BrowseDirectChildren
    }

    [DataContractAttribute]
    public class BrowseOptions : MediaLibOptions
    {
        /// <summary>
        /// One of BrowseMetaData and BrowseDirectChildren. The fist returns metadata pertaining to specified object, 
        /// the second returns metadata pertaining to its children. Default: BrowseDirectChildren the UPnP standard
        /// </summary>
        [DataMember]
        private BrowseFlagOptions Flag
        {
            get { return (BrowseFlagOptions)Enum.Parse(typeof(BrowseFlagOptions), (string)this.PropertyBag.GetPropertyFromPropertyBag("flag"), true); }
            set { this.PropertyBag.AddPropertyToPropertyBag("flag", Enum.GetName(typeof(BrowseFlagOptions), value).ToLowerInvariant()); }
        }

        /// <summary>
        /// The id of the parent object under which the browse is to be conducted, to be used in conjunction with either title or url
        /// </summary>
        public string Pid
        {
            get
            {
                return this.PropertyBag.GetPropertyFromPropertyBag("pid");
            }
            set
            {
                this.PropertyBag.AddPropertyToPropertyBag("pid", value);
            }
        }

        /// <summary>
        /// The title of the object under which the browse is to be conducted
        /// </summary>
        public string Title
        {
            get
            {
                return this.PropertyBag.GetPropertyFromPropertyBag("title");
            }
            set
            {
                this.PropertyBag.AddPropertyToPropertyBag("title", value);
            }
        }

        /// <summary>
        /// The resource URL of the object under which the browse is to be conducted
        /// </summary>
        public Uri Url
        {
            get
            {
                return new Uri(this.PropertyBag.GetPropertyFromPropertyBag("url"));
            }
            set
            {
                this.PropertyBag.AddPropertyToPropertyBag("url", value.AbsoluteUri);
            }
        }

        /// <summary>
        /// Constructor for SearchOptions
        /// It is expected that any specific search options will be set prior to sending this object
        /// </summary>
        /// <param name="query">The query string to search for.</param>        
        public BrowseOptions()
        {
            this.PropertyBag = new HelperTypes.PropertyBag();
            // This is the default format that is expected in the search response. Anything else will not be parsable.
            this.Format = TVersityResponse.DefaultFormat;
            // The following is the default setting.
            this.Flag = BrowseFlagOptions.BrowseDirectChildren;            
        }

        /// <summary>
        /// Constructor for SearchOptions
        /// It is expected that any specific search options will be set prior to sending this object
        /// </summary>
        /// <param name="query">The query string to search for.</param>        
        public BrowseOptions(MediaLibResponseItem searchRoot)
        {
            this.PropertyBag = new HelperTypes.PropertyBag();
            // This is the default format that is expected in the search response. Anything else will not be parsable.
            this.Format = TVersityResponse.DefaultFormat;
            // The following is the default setting.
            this.Flag = BrowseFlagOptions.BrowseDirectChildren;
            this.Id = searchRoot.Id;            
        }

        /// <summary>
        /// The resource URL of the object under which the browse is to be conducted, this is an alternative method to using id and it needs to be used in conjunction with pid
        /// </summary>
        /// <param name="url">The resource URL of the object under which the browse is to be conducted</param>
        /// <param name="pid">The id of the parent object under which the browse is to be conducted</param>
        public void UseUrl(Uri url, string pid)
        {
            this.Url = url;
            this.Pid = pid;
            // If the id was set, this will set it to an empty string
            this.PropertyBag.AddPropertyToPropertyBag("id", string.Empty);
        }

        /// <summary>
        /// The title of the object under which the browse is to be conducted, this is an alternative method to using id and it needs to be used in conjunction with pid
        /// </summary>
        /// <param name="title">The title</param>
        /// <param name="pid">The id of the parent object under which the browse is to be conducted</param>
        public void UseTitle(string title, string pid)
        {
            this.Title = title;
            this.Pid = pid;
            // If the id was set, this will set it to an empty string
            this.PropertyBag.AddPropertyToPropertyBag("id", string.Empty);
        }
    }
}